<?php

namespace haibao\app\data;

class ColumnArea extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\ColumnArea');
	}

	//CMS后台
	
	/**
	 * 增加地标
	 */
	public function addArea($columnAreaData){
		$this->add($columnAreaData);
		$this->setColunmAreaCache();
	}
	
	/**
	 * 获取地标最大sort
	 */
	public function getAreaMaxSort(){
		$sql = "select max(sort) as maxSort from ColumnArea where `Status` = %s";
		return  $this->query($sql, array(\haibao\app\model\data\ColumnCategory::STATUS_YES))->fetch_assoc();
	}
	
	/**
	 * 修改地标状态
	 */
	public function updateColumnAreaStatus($id, $status = \haibao\app\model\data\ColumnArea::STATUS_DELETE){
		$this->updateById($id, array(\haibao\app\model\filter\ColumnArea::CONDITION_FIELD_NAME_STATUS=>$status));
		$this->setColunmAreaCache();
	}
	
	/**
	 * 修改地标排序
	 */
	public function updateAreaSort($id, $arrSet){
		$this->updateById($id, $arrSet);
		$this->setColunmAreaCache();
	}
	
	/**
	 * 根据Id获得一条地标数据
	 */
	public function getAreaById($area_id){
		$areaList = array();
		$result = $this->query("select * from `ColumnArea` where AreaId = %s and Status = %s",array($area_id,\haibao\app\model\data\ColumnArea::STATUS_YES));
		$list = array();
		while($result->fetch_assoc()){
			$list['AreaId'] = $result->getData('AreaId');
			$areaList[] = $list;
		}
		return $areaList;
	}
	
	//公共
	public function getColunmArea(){
		$cache = new \haibao\app\cache\Column();
		$columnAreaList = $cache->getCacheByKey(\haibao\app\model\data\ColumnArea::CACHE_COLUMN_AREA_KEY);
		if(empty($columnAreaList)){
			$columnAreaList = $this->setColunmAreaCache();
		}
		return $columnAreaList;
	}
	
	private  function setColunmAreaCache(){
		$columnAreaList = array();
		$sql = "select a.Id,a.Name from ColumnArea c left join Area a on c.AreaId=a.Id where a.Display=%s and c.Status=%s order by c.Sort Asc ";
		$result = $this->query($sql, array(\haibao\app\model\data\Area::DISPLAY_ENABLE, \haibao\app\model\data\ColumnArea::STATUS_YES));
		$list = array();
		while ($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['Name'] = $result->getData('Name');
			$columnAreaList[] = $list;
		}
		$cache = new \haibao\app\cache\Column();
		$cache->setCache(\haibao\app\model\data\ColumnArea::CACHE_COLUMN_AREA_KEY, $columnAreaList);
		return $columnAreaList;
	}
}